---
--类名: NumInputView.lua
--创建日期: 2015-10-23
--创建人: 金剑
--功能描述: 拍卖行数值输入视图
--修改内容：
--修改日期:
--修改人:
--修改备注:
--

local OptionMenu = require("app.widget.optionmenu.OptionMenu")

local NumInputView = class("NumInputView",function()
    return display.newLayer("NumInputView")
end)

function NumInputView:ctor(pos, initValue, maxValue, maxNumCount, callback)
    self:initView(pos, initValue, maxValue, maxNumCount, callback)
end

function NumInputView:initView(pos, initValue, maxValue, maxNumCount, callback)
    --创建触摸
    local maskNode = self:createTouchMask()
    maskNode:setTouchEnabled(true)
    self:addChild(maskNode)

    --创建键盘
    local inputNode = cc.uiloader:load(Res.AuctionNumInputUI)
    local goodsNode = inputNode:getChildByName("num")
    goodsNode:retain()
    goodsNode:removeFromParent()

    self.defaultNode = ccui.Layout:create()
    self.defaultNode:setContentSize(goodsNode:getContentSize())
    self.defaultNode:addChild(goodsNode)
    goodsNode:release()

    --添加到界面上
    self.defaultNode:setPosition(pos)
    self:addChild(self.defaultNode)

    local function touchEvent_MaskTouched(sender, eventType)
        if eventType == ccui.TouchEventType.ended then
            self:removeFromParent()
        end
    end
    maskNode:addTouchEventListener(touchEvent_MaskTouched)

    -- local numStr = uitext:getString()
    local numValue = initValue

    local rootNode = self.defaultNode:getChildByName("num")

    local function touchEvent_AddNum(sender, eventType)
    	if eventType == ccui.TouchEventType.ended then
    		local tag = sender:getTag()

            local isChangeNum = true
            if maxNumCount then
                local num = math.pow(10, (maxNumCount - 1))
                if numValue > num then
                    isChangeNum = false
                    app:popMessage("sellPriceTips1", {maxNumCount})
                end
            end

            if isChangeNum then
                local num = numValue*10 + tag
                -- numValue = numValue*10 + tag
                if maxValue and num > maxValue then
                    app:popMessage("pluserror", {maxNumCount})
                    numValue = maxValue
                    callback(numValue)
                else
                    numValue = num
                    callback(numValue)
                end
                -- uitext:setString(numValue)

            end
        end
    end
    for i=1,10 do
    	local numBtn = rootNode:getChildByName(string.format("Button_%d",(i-1)))
    	numBtn:setTag(i-1)
    	numBtn:addTouchEventListener(touchEvent_AddNum)
    end

    local function touchEvent_Back(sender, eventType)
    	if eventType == ccui.TouchEventType.ended then
            numValue = math.floor(numValue/10)
            callback(numValue)
        end
    end
    local backBtn = rootNode:getChildByName("Button_1_2")
	backBtn:addTouchEventListener(touchEvent_Back)

	local function touchEvent_Ok(sender, eventType)
    	if eventType == ccui.TouchEventType.ended then
            self:removeFromParent()
        end
    end
    local okBtn = rootNode:getChildByName("Button_ok")
	okBtn:addTouchEventListener(touchEvent_Ok)

end

function NumInputView:createTouchMask()
    local maskNode = ccui.Layout:create()
    maskNode:setContentSize(display.width,display.height)
    return maskNode
end


function NumInputView:overturnBg()
    self.bg = self.defaultNode:getChildByName("num"):getChildByName("bg")
    self.bg:setRotation(180)
    self.defaultNode:setPosition(cc.p(self.defaultNode:getPositionX()+40,self.defaultNode:getPositionY()-210))
end


return NumInputView
